package com.linln.modules.integralmall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linln.common.utils.PathMappingUtil;
import com.linln.common.utils.UUIDUtil;
import com.linln.modules.integralmall.domain.RotationChart;
import com.linln.modules.integralmall.repository.RotationChartMapper;
import com.linln.modules.integralmall.service.RotationChartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;

/**
 * @author : pengliang
 * @version V1.0
 * @Project: timo
 * @Package com.linln.modules.integralmall.service.impl
 * @Descriptio /**
 * @Project: timo
 * @Package com.linln.modules.integralmall.service.impl
 * @Description: TODO
 * @date Date : 2020年12月15日 16:22
 */
@Service
public class RotationChartServiceImpl extends ServiceImpl<RotationChartMapper, RotationChart> implements RotationChartService {

    @Autowired
    private RotationChartMapper rotationChartMapper;

    @Override
    public boolean addRotationChart(String serviceId, List<MultipartFile> files) {
        for (int i = 0; i < files.size(); i++) {
            if (!"default".equals(files.get(i).getOriginalFilename())) {
                this.save(createRotationChart(serviceId, i, files.get(i)));
            }
        }
        return true;
    }

    @Override
    public boolean updateRotationChart(String serviceId, List<MultipartFile> files, List<Integer> status) {
        int delCount = 0;
        for (int i = 0; i < status.size(); i++) {
            if (status.get(i) == 0) {
                //添加
                if (!"default".equals(files.get(i).getOriginalFilename())) {
                    this.save(createRotationChart(serviceId, i, files.get(i)));
                }
            } else if (status.get(i) == 1) {
                //更新
                if (!"default".equals(files.get(i).getOriginalFilename())) {
                    RotationChart rotationChart = this.getOne(new QueryWrapper<RotationChart>().eq("ims_id", serviceId).eq("rc_order", i));
                    new File(PathMappingUtil.getPath(PathMappingUtil.IMGPATH, rotationChart.getRcAddress())).delete();
                    rotationChart.setRcAddress(PathMappingUtil.saveImg(PathMappingUtil.IMGPATH, PathMappingUtil.IMGPATHMAPPING, files.get(i)));
                    this.updateById(rotationChart);
                }
            } else if (status.get(i) == 2) {
                //删除
                RotationChart rotationChart = this.getOne(new QueryWrapper<RotationChart>().eq("ims_id", serviceId).eq("rc_order", i - delCount));
                String path = PathMappingUtil.getPath(PathMappingUtil.IMGPATH, rotationChart.getRcAddress());
                new File(path).delete();
                this.removeById(rotationChart.getRcId());
                rotationChartMapper.updateOrder(serviceId, i);
                delCount++;
            }
        }
        return true;
    }

    private RotationChart createRotationChart(String serviceId, int index, MultipartFile file) {
        RotationChart r = new RotationChart();
        r.setImsId(serviceId);
        r.setRcAddress(PathMappingUtil.saveImg(PathMappingUtil.IMGPATH, PathMappingUtil.IMGPATHMAPPING, file));
        r.setRcOrder(index);
        r.setRcId(UUIDUtil.randomUUID32());
        return r;
    }


    @Override
    public List<RotationChart> getRotationChatsByServiceId(String serviceId) {
        return this.list(new QueryWrapper<RotationChart>().eq("ims_Id", serviceId).orderByAsc("rc_order"));
    }

    @Override
    public boolean delRotationChatsByServiceId(String serviceId) {
        return this.remove(new QueryWrapper<RotationChart>().eq("ims_Id", serviceId));
    }


}
